import room from "@/mount/room"
import { MissonModel } from "@/mount/room/Misson/missonModel"
import { colorful, isInArray } from "@/utils"
import number from "../number"
/* 战争相关任务API */
export default {
    /* 个体机 */
    oaio:{
        /* 攻防一体机 */
        attack(roomName:string,disRoom:string,sw:boolean = true):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['攻防一体'])
            thisTask.disRoom = [disRoom]
            thisTask.bindData = {'ADIM':{num:1,bind:[]}}
            thisTask.switch = sw
            thisTask.relateCreep = ['ADIM']
            var labs = myRoom.memory.TaskMemory.labs
            if (sw)
            {
                if (!labs || labs.length < 4) return `[aio]实验室数量不足，攻防一体任务取消`
                /* 检查未被占用的实验室数量是否足够 */
                var NuN = []
                var NuR = ['XZHO2','XLHO2','XKHO2','XGHO2']
                var Data = {}
                for (var l of labs)
                {
                    if (!myRoom.memory.TaskData.OccupiedLab[l] && NuN.length < 4)
                    {
                        NuN.push(l)
                    }
                }
                if (NuN.length < 4) return `[aio]实验室空闲数量不足，攻防一体任务取消!`
                for (var n in NuN)
                {
                    var thisLab = Game.getObjectById(NuN[n]) as StructureLab
                    if (!thisLab) return `[aio]不存在lab，未知错误!`
                    var thisFlag = thisLab.pos.lookFor(LOOK_FLAGS)[0]
                    if (!thisFlag) return `[aio]lab${thisLab.id}上不存在旗帜!`
                    Data[thisFlag.name] = [thisLab.id,NuR[n],1]
                }
                thisTask.LabBind = Data
            }
            if (myRoom.mountMisson(thisTask)) return `[aio]房间${roomName}的攻防一体任务挂载成功!`
            return `[aio]房间${roomName}的攻防一体任务挂载失败!`
        },
        Cattack(roomName:string,disRoom?:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            if (!disRoom)
            {
                var result = myRoom.deleteMisson('CREEP','攻防一体')
                if (result) return `[aio]房间${roomName}的攻防一体任务删除成功!`
                return colorful(`[aio]房间${roomName}的攻防一体任务删除失败!`,'red')
            }
            else
            {
                for (var i of myRoom.memory.Task['CREEP'])
                {
                    if (i.Name == '攻防一体' && i.disRoom[0] == disRoom)
                    {
                        myRoom.deleteMisson_ID(i.ID)
                        return `[aio]房间${roomName}的攻防一体任务删除成功!`
                    }
                }
                return colorful(`[aio]房间${roomName}的攻防一体任务删除失败!`,'red')
            }
        },
        Achange(roomName:string,disRoom:string,newRoom:string)
        {
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            for (var i of myRoom.memory.Task['CREEP'])
                {
                    if (i.Name == '攻防一体' && i.disRoom[0] == disRoom)
                    {
                        i.disRoom = [newRoom]
                        return `[aio]房间${roomName}的攻防一体任务房间切换为${newRoom}成功!`
                    }
                }
        },
        /* 拆家一体机 */
        dismantle(roomName:string,disRoom:string,sw:boolean = true):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['拆家一体'])
            thisTask.disRoom = [disRoom]
            thisTask.bindData = {'DSIM':{num:1,bind:[]}}
            thisTask.switch = sw
            thisTask.relateCreep = ['DSIM']
            var labs = myRoom.memory.TaskMemory.labs
            if (sw)
            {
                if (!labs || labs.length < 4) return `[aio]实验室数量不足，拆墙一体任务取消`
                /* 检查未被占用的实验室数量是否足够 */
                var NuN = []
                var NuR = ['XZHO2','XLHO2','XZH2O','XGHO2']
                var Data = {}
                for (var l of labs)
                {
                    if (!myRoom.memory.TaskData.OccupiedLab[l] && NuN.length < 4)
                    {
                        NuN.push(l)
                    }
                }
                if (NuN.length < 4) return `[aio]实验室空闲数量不足，拆墙一体任务取消!`
                for (var n in NuN)
                {
                    var thisLab = Game.getObjectById(NuN[n]) as StructureLab
                    if (!thisLab) return `[aio]不存在lab，未知错误!`
                    var thisFlag = thisLab.pos.lookFor(LOOK_FLAGS)[0]
                    if (!thisFlag) return `[aio]lab${thisLab.id}上不存在旗帜!`
                    Data[thisFlag.name] = [thisLab.id,NuR[n],1]
                }
                thisTask.LabBind = Data
            }
            if (myRoom.mountMisson(thisTask)) return `[aio]房间${roomName}的拆墙一体任务挂载成功!`
            return `[aio]房间${roomName}的拆墙一体任务挂载失败!`
        },
        Cdismantle(roomName:string,disRoom?:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            if (!disRoom)
            {
                var result = myRoom.deleteMisson('CREEP','拆家一体')
                if (result) return `[aio]房间${roomName}的拆墙一体任务删除成功!`
                return colorful(`[aio]房间${roomName}的拆墙一体任务删除失败!`,'red')
            }
            else
            {
                for (var i of myRoom.memory.Task['CREEP'])
                {
                    if (i.Name == '拆家一体' && i.disRoom[0] == disRoom)
                    {
                        myRoom.deleteMisson_ID(i.ID)
                        return `[aio]房间${roomName}的拆墙一体任务删除成功!`
                    }
                }
                return colorful(`[aio]房间${roomName}的拆墙一体任务删除失败!`,'red')
            }
        },
        Dchange(roomName:string,disRoom:string,newRoom:string)
        {
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            for (var i of myRoom.memory.Task['CREEP'])
                {
                    if (i.Name == '拆家一体' && i.disRoom[0] == disRoom)
                    {
                        i.disRoom = [newRoom]
                        return `[aio]房间${roomName}的攻防一体任务房间切换为${newRoom}成功!`
                    }
                }
        },
        /* 防御红球 */
        defend(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['红球防御'])
            thisTask.bindData = {'defend-red':{num:1,bind:[]}}
            thisTask.relateCreep = ['defend-red']
            var labs = myRoom.memory.TaskMemory.labs
            if (!labs || labs.length < 2) return `[aio]实验室数量不足，红球防御任务取消`
            /* 检查未被占用的实验室数量是否足够 */
            var NuN = []
            var NuR = ['XZHO2','XUH2O']
            var Data = {}
            for (var l of labs)
            {
                if (!myRoom.memory.TaskData.OccupiedLab[l] && NuN.length < 2)
                {
                    NuN.push(l)
                }
            }
            if (NuN.length < 2) return `[aio]实验室空闲数量不足，红球防御任务取消!`
            for (var n in NuN)
            {
                var thisLab = Game.getObjectById(NuN[n]) as StructureLab
                if (!thisLab) return `[aio]不存在lab，未知错误!`
                var thisFlag = thisLab.pos.lookFor(LOOK_FLAGS)[0]
                if (!thisFlag) return `[aio]lab${thisLab.id}上不存在旗帜!`
                Data[thisFlag.name] = [thisLab.id,NuR[n],1]
            }
            thisTask.LabBind = Data
            if (myRoom.mountMisson(thisTask)) return `[aio]房间${roomName}的红球防御任务挂载成功!`
            return `[aio]房间${roomName}的红球防御任务挂载失败!`
        },
        Cdefend(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '红球防御')
                {
                    if(myRoom.deleteMisson_ID(i.ID)) return colorful(`[repair]房间${roomName}的红球防御任务已经删除!`,'green')
                }
            }
            return `[repair] 房间${roomName}未找到红球防御任务!`

        },
        /* 拆迁黄球 */
        fuck(roomName:string,disRoom:string,num:number):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到房间${roomName},请确认是否是自己的房间`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['黄球拆迁'])
            thisTask.bindData = {'dismantle-yellow':{num:num,bind:[]}}
            thisTask.relateCreep = ['dismantle-yellow']
            thisTask.disRoom = [disRoom]
            var labs = myRoom.memory.TaskMemory.labs
            if (!labs || labs.length < 2) return `[aio]实验室数量不足，红球防御任务取消`
            /* 检查未被占用的实验室数量是否足够 */
            var NuN = []
            var NuR = ['XZHO2','XZH2O']
            var Data = {}
            for (var l of labs)
            {
                if (!myRoom.memory.TaskData.OccupiedLab[l] && NuN.length < 2)
                {
                    NuN.push(l)
                }
            }
            if (NuN.length < 2) return `[aio]实验室空闲数量不足，黄球拆迁任务取消!`
            for (var n in NuN)
            {
                var thisLab = Game.getObjectById(NuN[n]) as StructureLab
                if (!thisLab) return `[aio]不存在lab，未知错误!`
                var thisFlag = thisLab.pos.lookFor(LOOK_FLAGS)[0]
                if (!thisFlag) return `[aio]lab${thisLab.id}上不存在旗帜!`
                Data[thisFlag.name] = [thisLab.id,NuR[n],1]
            }
            thisTask.LabBind = Data
            if (myRoom.mountMisson(thisTask)) return `[aio]房间${roomName}的黄球拆迁任务挂载成功!`
            return `[aio]房间${roomName}的黄球拆迁任务挂载失败!`
        },
        Cfuck(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '黄球拆迁')
                {
                    if(myRoom.deleteMisson_ID(i.ID)) return colorful(`[repair]房间${roomName}的黄球拆迁任务已经删除!`,'green')
                }
            }
            return `[repair] 房间${roomName}未找到黄球拆迁任务!`
        },
        Fchange(roomName:string,disRoom:string,newRoom:string)
        {
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[aio]未找到目标房间${roomName},请确认是否是自己的房间!`
            for (var i of myRoom.memory.Task['CREEP'])
                {
                    if (i.Name == '黄球拆迁' && i.disRoom[0] == disRoom)
                    {
                        i.disRoom = [newRoom]
                        return `[aio]房间${roomName}的黄球拆迁任务房间切换为${newRoom}成功!`
                    }
                }
            return `[aio]换房间失败!`
        }
    }
}